package com.ruoyi.project.mahjong.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.Commodity;
import com.ruoyi.project.mahjong.service.ICommodityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/mahjong/commodity")
public class CommodityController extends BaseController
{
    @Autowired
    private ICommodityService commodityService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commodity commodity)
    {
        if(ObjectUtil.isNotNull(commodity.getCommodityName())){
            commodity.setCommodityName(commodity.getCommodityName().trim());
        }
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        commodity.setShopId(shopId.toString());
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        return getDataTable(list);
    }
    @GetMapping("/listallCommodity")
    public AjaxResult listallCommodity(Commodity commodity)
    {

        List<Commodity> list = commodityService.selectCommodityList(commodity);
        return AjaxResult.success(list);
    }

    /**
     * 查询
     * @param commodity
     * @return
     */
    @GetMapping("/lishavJftPage")
    public AjaxResult listPage(Commodity commodity)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        commodity.setShopId(shopId.toString());
        List<Commodity> list = commodityService.lishavJftPage(commodity);
        return AjaxResult.success(list);
    }



    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Commodity commodity)
    {
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        ExcelUtil<Commodity> util = new ExcelUtil<Commodity>(Commodity.class);
        return util.exportExcel(list, "commodity");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(commodityService.selectCommodityById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commodity commodity)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        commodity.setShopId(shopId.toString());
        return toAjax(commodityService.insertCommodity(commodity));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commodity commodity)
    {
        return toAjax(commodityService.updateCommodity(commodity));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('mahjong:commodity:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commodityService.deleteCommodityByIds(ids));
    }
}
